package com.lg.okframe.study.designmode;

/**
 * 适配器模式
 * Created by LG on 2018/8/20.
 */

public class AdapterMode {

    /**
     * 对象/类 适配器模式
     */
    static class ClassAdapter {

        public void use() {
            Xiaomi mi1 = new Xiaomi();
            mi1.takeAlong();
            mi1.store();
        }

        public interface Usb {
            void store();

            void takeAlong();
        }

        public class Phone {
            public void call() {
                System.out.println("Phone call");
            }

            public void sms() {
                System.out.println("Phone sms");
            }

            public void takeAlong() {
                System.out.println("Phone takeAlong");
            }
        }

        /**
         * 类的适配器模式
         * phone + Usb
         * 将Phone的功能扩展到Usb里
         */
        public class Xiaomi extends Phone implements Usb {

            @Override
            public void store() {
                // TODO Auto-generated method stub
                System.out.println("store implements usb");
            }
        }

        /**
         * 对象的适配器模式
         */

        public class XiaomiWrapper implements Usb {

            private Phone phone;

            public XiaomiWrapper(Phone phone) {

                this.phone = phone;
            }

            @Override
            public void store() {
                // TODO Auto-generated method stub
                System.out.println("store implements usb");

            }

            @Override
            public void takeAlong() {
                // TODO Auto-generated method stub
                phone.takeAlong();
            }

        }


        /**
         * 接口适配器
         */
        static class ImplementAdapter {

            public void use() {

                Phone1 p1 = new Phone1();
                Phone2 p2 = new Phone2();
                p1.store();
                p2.takeAlong();
            }

            /**
             * 接口的适配器模式
             * 1.借助于一个抽象类，该抽象类实现了该接口，实现了所有的方法
             * 2.继承类可以选择性的实现接口中的方法
             */
            public abstract class AbsPhone implements Usb {

                public void store() {
                    System.out.println("AbsPhone implements usb's store methond");
                }

                public void takeAlong() {
                    System.out.println("AbsPhone implements usb's takeAlong methond");
                }
            }


            public class Phone1 extends AbsPhone {

                public void call() {
                    System.out.println("Phone1 call");
                }

                public void sms() {
                    System.out.println("Phone1 sms");
                }

                public void store() {
                    System.out.println("Phone1 need usb's store methond");
                }

            }


            public class Phone2 extends AbsPhone {

                public void call() {
                    System.out.println("Phone2 call");
                }

                public void sms() {
                    System.out.println("Phone2 sms");
                }

                public void takeAlong() {
                    System.out.println("Phone2 need usb's takeAlong methond");
                }
            }
        }
    }

}
